﻿using UnityEngine;
using System.Collections;

public class ArmoryScrollBar : MonoBehaviour {

    bool bDrag;
    int iListCount;

    bool bSmallThanMinBarSize;   // 본 사이즈가 최소사이즈보다 작을때 true

    int iNumOfMaxLists;  //한번에 창에보이는 최대 개수

    float fMaxBarSize; // 바 길이 최대
    float fMinBarSize;

    float fBarDefaultYPos;  //최초 y위치
    float fBarYPosPlus;        // 개수 증가할때마다 +되는 Y위치값

    float fMinBarPos;

	// Use this for initialization
	void Start () {
        bSmallThanMinBarSize = false;
        bDrag = false;

        iNumOfMaxLists = 12;
        fMaxBarSize = 0.71f;
        fMinBarSize = 0.05f;

        fBarDefaultYPos = 8.87f;
        fBarYPosPlus = 2.07f;

        fMinBarPos = 41.84f;

     
	}
	
	// Update is called once per frame
	void Update () {
        if (bDrag)
        {
            if (Input.GetMouseButton(0))
            {// Drag!
                DragObject();
            }
        }
	}

    void OnPress(bool isDown)
    {
        if (isDown)
            bDrag = true;
        else
            bDrag = false;
    }

    void DragObject()
    {
        if (bSmallThanMinBarSize)
        {
            transform.position = new Vector3(transform.position.x, UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition).y);

            if (transform.localPosition.y > fMinBarPos)
                transform.localPosition = new Vector3(transform.localPosition.x, fMinBarPos);
            else if (transform.localPosition.y < -fMinBarPos)
                transform.localPosition = new Vector3(transform.localPosition.x, -fMinBarPos);
            else
                transform.position = new Vector3(transform.position.x, UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition).y);
        }
        else
        {
            transform.position = new Vector3(transform.position.x, UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition).y);

            if (transform.localPosition.y > fBarDefaultYPos + (((float)iListCount - (float)iNumOfMaxLists) * fBarYPosPlus))
                transform.localPosition = new Vector3(transform.localPosition.x, (((float)iListCount - (float)iNumOfMaxLists) * fBarYPosPlus) + fBarDefaultYPos);
            else if (transform.localPosition.y < fBarDefaultYPos - (((float)iListCount - (float)iNumOfMaxLists) * fBarYPosPlus))
                transform.localPosition = new Vector3(transform.localPosition.x, fBarDefaultYPos - (((float)iListCount - (float)iNumOfMaxLists) * fBarYPosPlus));
            else
                transform.position = new Vector3(transform.position.x, UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition).y);
        }
    }

    public void ScrollBarReset()
    {
        iListCount = transform.parent.GetComponent<ArmoryPaper>().m_WeaponGroupList.Count;

        if (iListCount > iNumOfMaxLists)
        {
            gameObject.SetActive(true);
            transform.localScale = new Vector3(transform.localScale.x, fMaxBarSize - (((float)iListCount / (float)iNumOfMaxLists) - 1f) / 2f);
            transform.localPosition = new Vector3(transform.localPosition.x, (((float)iListCount - (float)iNumOfMaxLists) * fBarYPosPlus) + fBarDefaultYPos);
            GameObject.Find("ScrollObjects").GetComponent<ArmoryScrollObjects>().fBarFirstPosY = transform.localPosition.y;
            if (transform.localScale.y < fMinBarSize)
            {
                transform.localScale = new Vector3(transform.localScale.x, fMinBarSize);
                transform.localPosition = new Vector3(transform.localPosition.x, fMinBarPos);
                bSmallThanMinBarSize = true;
                GameObject.Find("ScrollObjects").GetComponent<ArmoryScrollObjects>().bSmallThanMinBarSize = true;
            }
        }
        else
        {
            gameObject.SetActive(false);
        }
    
    }
}
